/**
 * ConvertUtil.java
 *
 * Copyright (c) 2009 viorelt All Rights Reserved.
 *
 * This source file may not be copied, modified or redistributed,
 * in whole or in part, in any form or for any reason, without the express
 * written consent of viorelt .
 **/
package ro.prjmanager.core.util;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class ConvertUtil {
    static Map<Class, Class> PRIMITIVE_TO_WRAPPED = new HashMap<Class, Class>();

    static {
        PRIMITIVE_TO_WRAPPED.put(boolean.class, java.lang.Boolean.class);
        PRIMITIVE_TO_WRAPPED.put(char.class, java.lang.Character.class);
        PRIMITIVE_TO_WRAPPED.put(byte.class, java.lang.Byte.class);
        PRIMITIVE_TO_WRAPPED.put(short.class, java.lang.Short.class);
        PRIMITIVE_TO_WRAPPED.put(int.class, java.lang.Integer.class);
        PRIMITIVE_TO_WRAPPED.put(long.class, java.lang.Long.class);
        PRIMITIVE_TO_WRAPPED.put(float.class, java.lang.Float.class);
        PRIMITIVE_TO_WRAPPED.put(double.class, java.lang.Double.class);

    }


    private static Boolean castToBoolean(Integer integer){
        return integer == 1;
    }

    private static Boolean castToBoolean(Long aLong){
        return aLong.equals(Long.valueOf(1));
    }


    private static Integer castToInteger(Boolean b){
        if (b)
            return 1;
        else return 0;
    }
    private static Integer castToInteger(Long aLong){
        return aLong.intValue();
    }

    private static Integer castToInteger(BigDecimal bigDecimal){
        return bigDecimal.intValue();
    }

    private static Long castToLong(Integer integer){
        return Long.valueOf(integer);
    }

    private static Long castToLong (BigDecimal bigDecimal){
        return bigDecimal.longValue();
    }

    private static BigDecimal castToBigDecimal(Integer integer){
        return BigDecimal.valueOf(integer);
    }

    private static BigDecimal castToBigDecimal(Long aLong){
        return BigDecimal.valueOf(aLong);
    }

    public static Object cast(Object value,Class toClazz) throws Exception {
        if(value==null)
            return null;
        if(toClazz.equals(value.getClass()))
            return value;
        if(toClazz.isPrimitive())
            toClazz=PRIMITIVE_TO_WRAPPED.get(toClazz);
        if(toClazz.equals(Boolean.class)){
           if(value instanceof Integer)
            return castToBoolean((Integer) value);
           else if(value instanceof Long)
            return castToBoolean((Long) value);
            else if(value instanceof String){
               return castToBoolean(Integer.valueOf((String)value));
           }

        }else if(toClazz.equals(Integer.class)){
            if(value instanceof Boolean)
                return castToInteger((Boolean)value);
            else if(value instanceof Long)
                return castToInteger((Long)value);
            else if(value instanceof BigDecimal)
                return castToInteger((BigDecimal)value);

        }else if(toClazz.equals(Long.class)){
             if(value instanceof Integer)
                return castToLong((Integer)value);
            else if(value instanceof BigDecimal)
                return castToLong((BigDecimal)value);
        }else if(toClazz.equals(BigDecimal.class)){
            if(value instanceof Integer)
                return castToBigDecimal((Integer)value);
            else if(value instanceof Long)
                return castToBigDecimal((Long)value);
        }
        throw new Exception("Cast not founded!!!");
    }


}
